import { _decorator, Button, Component, Label, Node } from 'cc';
import { DataManager } from '../DataManager';
const { ccclass, property } = _decorator;

@ccclass('SettlementResultController')
export class SettlementResultController extends Component {
  @property(Label)
  content: Label;
  @property(Label)
  result: Label;

  start() {}

  close() {
    this.node.active = false;
  }

  render() {
    this.node.active = true;
    const gameData = DataManager.instance.gameData;
    const role = DataManager.instance.role;
    const { winner, clients } = gameData;
    const myClient = clients.find((client) => client.role === role);
    const enemyClient = clients.find((client) => client.role !== role);
    const mySelectedCard = myClient.cards.find(
      (card) => card.cardIndex === myClient.selectedCard
    );
    const enemySelectedCard = enemyClient.cards.find(
      (card) => card.cardIndex === enemyClient.selectedCard
    );

    this.content.string = `${mySelectedCard.title} VS ${enemySelectedCard.title}`;
    if (gameData.gameStatus === 'end') {
      this.content.string = `${myClient.score} - ${enemyClient.score}`;
    }
    let result = '';
    switch (winner) {
      case role:
        result = '胜利!';
        break;
      case enemyClient.role:
        result = '失败';
        break;
      default:
        result = '平局';
    }
    this.result.string = result;
  }

  update(deltaTime: number) {}
}
